<template>
  <div>
    <el-dialog title="增加优惠券" :visible.sync="showAddCouponDialog" center width="770px">
      <el-form :model="couponData" :rules="rules" ref="couponData">

        <el-form-item label="优惠券所属活动" :label-width="formLabelWidth">
          <el-select v-model="couponData.activity_id" filterable placeholder="请选择">
            <el-option
              v-for="item in activityList"
              :key="item.activity_id"
              :label="item.name"
              :value="item.activity_id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="减免金额" :label-width="formLabelWidth" prop="deduction_amount">
          <el-input v-model="couponData.deduction_amount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开始时间" :label-width="formLabelWidth">
            <el-date-picker v-model="couponData.start_date" type="datetime" placeholder="选择日期">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" :label-width="formLabelWidth">
          <el-date-picker v-model="couponData.end_date" type="datetime" placeholder="选择日期"></el-date-picker>
        </el-form-item>



<!--        <el-form-item label="领取日期" :label-width="formLabelWidth">-->
<!--          <el-date-picker v-model="couponData.customer_receive_date" type="datetime" placeholder="选择日期">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="couponData.used" :label="0">未使用</el-radio>
          <el-radio v-model="couponData.used" :label="1">已使用</el-radio>
        </el-form-item>

<!--        <el-form-item label="使用日期" :label-width="formLabelWidth">-->
<!--          <el-date-picker v-model="couponData.used_date" type="datetime" placeholder="选择日期"></el-date-picker>-->
<!--        </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelSubmit">取 消</el-button>
        <el-button type="primary" @click="submitCoupon('couponData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import store from '@/store'
  import {add} from '@/api/thk/coupon'

  import {dateFormat} from '@/utils/dateFormat'

  export default {
    name: 'addCoupon',

    data() {
      return {
        showAddCouponDialog: false,
        formLabelWidth: '220px',

        /**
         activity_id	            活动id
         deduction_amount	        减免金额
         start_date	datetime	    开始时间
         end_date	datetime	      结束时间
         customer_id	            领取用户0为未被领取
         customer_receive_date	  领取日期
         used                     是否已使用0未使用，1已使用
         used_date	              使用日期

         */

        couponData: {
          activity_id: '',                // 活动创建者id
          deduction_amount: '',           // 减免金额
          start_date: '',                 // 开始时间
          end_date: '',                   // 结束时间
          customer_id: 0,                 // 领取用户（0为未领取）
          customer_receive_date: '',      // 领取日期
          used: 0,                        // 是否使用，0未使用， 1已使用
          used_date: '',                  // 使用日期
        },

        customerList: [],
        activityList: [],

        rules: {
          // 减免金额必须
          deduction_amount:[
            { required: true, message: '减免金额为必填项', trigger: 'blur' },
          ],
          activity: [
            { required: true, message: '请选择活动区域', trigger: 'change' }
          ],
        }
      }
    },




    methods: {
      submitCoupon(couponData){
        console.log('submitCoupon')
        this.$refs[couponData].validate((valid) => {
          if (valid) {
            this.couponData.start_date = dateFormat("YYYY-mm-dd HH:MM:SS", new Date(this.couponData.start_date))
            this.couponData.end_date = dateFormat("YYYY-mm-dd HH:MM:SS", new Date(this.couponData.end_date))
            // this.couponData.customer_receive_date =
            //   dateFormat("YYYY-mm-dd HH:MM:SS", new Date(this.couponData.customer_receive_date))
            // this.couponData.used_date = dateFormat("YYYY-mm-dd HH:MM:SS", new Date(this.couponData.used_date))


            console.log('========= couponAdd ============')
            console.log(this.couponData)

            add(store.getters.token,this.couponData).then(result => {
              // 增加成功
              this.$parent.getCouponList()
              this.showAddCouponDialog = !this.showAddCouponDialog
              this.$message.success("增加成功!")
            }).catch(error => {
              this.$message('增加失败!')
              console.log(error)
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },

      cancelSubmit(activityList){
        this.activityList = activityList
        this.showAddCouponDialog = !this.showAddCouponDialog

      },
      //
      // cancelSubmit(customerList, storeList){
      //   this.customerList = customerList
      //   this.storeList = storeList
      //   this.showAddCouponDialog = !this.showAddCouponDialog
      //
      // },




    }
  }
</script>
